package com.luobu.javaee.service;

import com.luobu.javaee.dao.ArchiveMapper;
import com.luobu.javaee.dao.DocumentMapper;
import com.luobu.javaee.domain.Archive;
import com.luobu.javaee.domain.Document;
import com.luobu.javaee.domain.dto.DocumentDto;
import com.luobu.javaee.domain.dto.DocumentResDto;
import com.luobu.javaee.util.FileDelete;
import com.luobu.javaee.util.FileIO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArchiveService {
    @Autowired
    ArchiveMapper archiveMapper;

    @Autowired
    DocumentMapper documentMapper;

    @Autowired
    DocumentService documentService;

    public Long newArchive(List<Document> docList, String archiveName, Long userId) throws IOException {
        // 创建新的文集
        Archive archive = new Archive();
        archive.setArchiveName(archiveName);
        archive.setUserId(userId);
        archiveMapper.insert(archive);
        List<Long> archiveIds = archiveMapper.findByUserIdAndArchiveName(userId, archiveName);
        Long archiveId = archiveIds.get(0);
        setArchiveId(docList, archiveId);
        return archiveId;
    }

    public List<DocumentResDto> searchArchive(Long archiveId) throws IOException {
        // 获取id为archiveId的文集的所有文档的信息并返回
        List<Document> documentList = documentMapper.findByArchiveId(archiveId);
        List<DocumentResDto> documentResDtoList = new ArrayList<>();
        for (Document document : documentList){
            DocumentResDto documentResDto = new DocumentResDto();
            String docName = document.getDocName();
            Long userId = document.getUserId();
            Long docId = document.getId();

            String filePath = FileIO.getFilePath(userId, docName, archiveId);

            documentResDto.setDocId(docId);
            documentResDto.setDocName(docName);
            documentResDto.setDocContent(FileIO.readFile(filePath));
            documentResDtoList.add(documentResDto);
        }
        return documentResDtoList;
    }

    public void deleteDocFromArchive(Long archiveId, Long docId){
        // 将docId的文档从archiveId中删除
        documentMapper.updateArchiveIdBydocId(docId);
    }

    // 将docList包含的所有文档的archiveId属性设置为archiveId参数
    public void setArchiveId(List<Document> docList, Long archiveId) throws IOException {
        for (Document document : docList)
        {
            String resUrl = FileIO.getFilePath(document.getUserId(), document.getDocName(), document.getArchiveId());
            String desUrl = FileIO.getFilePath(document.getUserId(), document.getDocName(), archiveId);
            FileIO.resourcePathCheck(desUrl);
            FileIO.moveFile(resUrl, desUrl);

            resUrl = FileIO.getPictureDirPath(document.getUserId(), document.getDocName(), document.getArchiveId());
            desUrl = FileIO.getPictureDirPath(document.getUserId(), document.getDocName(), archiveId);
            FileIO.resourcePathCheck(desUrl);
            FileIO.moveFolder(resUrl, desUrl);

            documentMapper.updateArchiveIdByUserIdAndDocNameAndArchiveId(
                    document.getUserId(), document.getDocName(), document.getArchiveId(), archiveId);
        }
    }

    // 获取userId对应的用户的所有文集编号和名称
    public List<Archive>getUserArchiveList(Long userId)
    {
        return archiveMapper.findByUserId(userId);
    }

    // 删除archiveId对应的文集
    public void deleteArchive(Long archiveId) throws IOException {
        List<Document> list = documentMapper.findByArchiveId(archiveId);
        setArchiveId(list, -1L);

        Archive archive = archiveMapper.selectById(archiveId);
        String fileArchivePath = FileIO.getFileArchivePath(archive.getUserId(), archiveId);
        FileDelete.deleteDiretory(fileArchivePath);
        String picArchivePath = FileIO.getPicArchivePath(archive.getUserId(), archiveId);
        FileDelete.deleteDiretory(picArchivePath);

        archiveMapper.deleteById(archiveId);
    }

}
